package gui.android;

import java.util.Arrays;

import database.dbCharacterImpl;
import database.dbTempImpl;
import perv_comp.abbs.cok.R;

import ability.Ability;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import animal.Adler;
import animal.Animal;
import animal.Elefant;

public class GroundOFGame extends Activity implements OnClickListener {

	private ImageButton imageButton1;
	private ImageButton imageButton2;
	private ImageButton imageButton3;
	private ImageButton imageButton4;
	
	SQLiteDatabase mydb;
	private String DBNAME = "PERSONS.db";
	private String TABLE = "MY_TABLE";
	int playerInt;
	int round=1;
	int gameID;
	private ImageView myAnimalView;
	private ImageView enemyAnimalView;
	private TextView hpFullMe;
	private TextView hpFullEnemy;
	private TextView showDmgMe;
	private TextView showDmgEnemy;
	private TextView showStatus;
	private TextView showEvent;
	private ProgressBar lebensBalkenMeProg;
	private RotatedProgressBar lebensBalkenEnemyProg;
	private int activeRolle = 1;
	private Animal myAnimal;
	private Animal enemyAnimal;
	String enemyAbility="";
	int myAbility;
	int lebenspunktMeBegin;
	int lebenspunktEnemyBegin=0;
	int lebenspunktMeNow;
	int lebenspunktEnemyNow=0;
	Bitmap[] staticAnimationMe;
	Bitmap[] staticAnimationEnemy;
	UIUpdater mUIUpdater;
	uiAnimator staticAnimator;
	int globalFrame=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_ground_ofgame);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
		
		initializeTextViews();
		initializeButtons();
		initializeImageViews();
		initializeProgressBars();

		readFromDB();

		loadSelectedAnimal();
		
		loadButtons();
		loadEnemyAnimal();
		loadRollen();
		
		

		 staticAnimator = new uiAnimator(new Runnable() {
			@Override
			public void run() {
				showStaticAnimation();
			}
		});
		 staticAnimator.startUpdates();
	}

	private void loadRollen() {
		if(activeRolle==1){
			lebensBalkenMeProg.setBackgroundResource(R.drawable.rollen_icon_offensive);
			lebensBalkenEnemyProg.setBackgroundResource(R.drawable.rollen_icon_defensive_m);
		} else if(activeRolle==2){
			lebensBalkenMeProg.setBackgroundResource(R.drawable.rollen_icon_defensive);
			lebensBalkenEnemyProg.setBackgroundResource(R.drawable.rollen_icon_offensive_m);
				}
		
		
	}
	
	private void playAnimation() {
		
		
		myAnimation();
		enemyAnimation();
		
	}


	private void enemyAnimation() {
		
		
		
	}
	
	private void showStaticAnimation() {
		myAnimalView.setImageBitmap(staticAnimationMe[globalFrame]);
		enemyAnimalView.setImageBitmap(staticAnimationEnemy[globalFrame]);
		globalFrame++;
		if (globalFrame==25){
			globalFrame=0;
		}
		
	}

	private void myAnimation() {
		// TODO Auto-generated method stub
	
	}

	private void initializeProgressBars() {
		lebensBalkenMeProg = (ProgressBar) findViewById(R.id.lebensBalkenMe);
		lebensBalkenMeProg.setProgress(100);

		lebensBalkenEnemyProg = (RotatedProgressBar) findViewById(R.id.lebensBalkenEnemy);
		lebensBalkenEnemyProg.draw(new Canvas());
		lebensBalkenEnemyProg.setProgress(100);

	}

	private void initializeImageViews() {
		myAnimalView = (ImageView) findViewById(R.id.myAnimalView);
		enemyAnimalView = (ImageView) findViewById(R.id.enemyAnimalView);

	}

	private void initializeButtons() {
		imageButton1 = (ImageButton) findViewById(R.id.abilityButton1);
		imageButton2 = (ImageButton) findViewById(R.id.abilityButton2);
		imageButton3 = (ImageButton) findViewById(R.id.abilityButton3);
		imageButton4 = (ImageButton) findViewById(R.id.abilityButton4);

		imageButton1.setOnClickListener(this);
		imageButton2.setOnClickListener(this);
		imageButton3.setOnClickListener(this);
		imageButton4.setOnClickListener(this);

	}
	
	private Bitmap[] getFrames(int image, int frames) {
		 Bitmap bMap = BitmapFactory.decodeResource(getResources(), image);
		 Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 4000, 160, true);
		 Bitmap[] myAnimation = new Bitmap[frames];
		 for (int i = 0; i < frames; i++) {
			myAnimation[i]=Bitmap.createBitmap(bMapScaled, i*160, 0, 160, 160);
		}
		 return myAnimation;
	}

	private void initializeTextViews() {

		hpFullMe = (TextView) findViewById(R.id.hpViewMe);
		hpFullEnemy = (TextView) findViewById(R.id.hpViewEnemy);

		showDmgMe = (TextView) findViewById(R.id.dmgTextViewMe);
		showDmgEnemy = (TextView) findViewById(R.id.dmgTextViewEnemy);
		showStatus = (TextView) findViewById(R.id.statusTextView);
		showEvent = (TextView) findViewById(R.id.eventTextView);
		hpFullMe.setText("");
		hpFullEnemy.setText("");
		showDmgMe.setText("");
		showDmgEnemy.setText("");
		showStatus.setText("");
		showEvent.setText("");

	}

	private void loadButtons() {
		imageButton1.setImageDrawable(getResources().getDrawable(
				myAnimal.getActivAbility()[0].getIconLink()));
		imageButton2.setImageDrawable(getResources().getDrawable(
				myAnimal.getActivAbility()[1].getIconLink()));
		imageButton3.setImageDrawable(getResources().getDrawable(
				myAnimal.getActivAbility()[2].getIconLink()));
		imageButton4.setImageDrawable(getResources().getDrawable(
				myAnimal.getActivAbility()[3].getIconLink()));
		imageButton1.setVisibility(1);
		imageButton2.setVisibility(1);
		imageButton3.setVisibility(1);
		imageButton4.setVisibility(1);

	}

	private void readFromDB() {

		try {
			mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
			Cursor allrows = mydb.rawQuery("SELECT * FROM " + TABLE, null);
			allrows.moveToFirst();
			myAnimal=new Animal().getAnimalOverID(allrows.getInt(2));
			enemyAnimal=new Animal().getAnimalOverID(allrows.getInt(5));
			activeRolle = allrows.getInt(3);
			playerInt = activeRolle;
			myAnimal.setActivAbility(activeRolle);
			gameID=allrows.getInt(6);
			mydb.close();
			allrows.close();
		} catch (Exception e) {

		}

	}

	private void loadSelectedAnimal() {
		myAnimalView.setImageDrawable(getResources().getDrawable(
				myAnimal.getMyAnimalGrafik()));
		lebenspunktMeBegin = myAnimal.getInitVita() * 100;
		hpFullMe.setText(String.valueOf(lebenspunktMeBegin) + " / "
				+ String.valueOf(lebenspunktMeBegin));
		staticAnimationMe = getFrames(myAnimal.getStaticAnimationMe(), 25);
		

	}

	private void loadEnemyAnimal() {
		
		enemyAnimalView.setImageDrawable(getResources().getDrawable(
				enemyAnimal.getEnemyAnimalGrafik()));
		lebenspunktEnemyBegin = enemyAnimal.getInitVita() * 100;
		lebenspunktEnemyNow= enemyAnimal.getInitVita() * 100;
		hpFullEnemy.setText(String.valueOf(lebenspunktEnemyBegin) + " " + "/ "
				+ String.valueOf(lebenspunktEnemyBegin));
		staticAnimationEnemy = getFrames(enemyAnimal.getStaticAnimationEnemy(), 25);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ground_ofgame, menu);
		return true;
	}

	private void execute(Ability ability) {
		hideButtons();
		setMyAbility(ability);
		
	}

	private void setMyAbility(Ability ability) {
		myAbility=ability.getId();
		dbTempImpl dbT = new dbTempImpl();
		Log.e("dbSetter:",String.valueOf(gameID)+" "
		+String.valueOf(round)+" "+String.valueOf(myAbility)+" "+String.valueOf(playerInt));
		if(playerInt==1){
			dbT.setFaehigkeitS1(gameID, round, myAbility);
		}if (playerInt==2){
			dbT.setFaehigkeitS2(gameID, round, myAbility);
		}
		
	}
	
	private void getEnemyAbility() {
		dbTempImpl dbT = new dbTempImpl();
		
		if(playerInt==1){
//			enemyAbility=dbT.getFaehigkeitS2(gameID);	
			}if (playerInt==2){
//			enemyAbility=dbT.getFaehigkeitS1(gameID);
			}
			
		Log.e("enemyAbil",enemyAbility);
	}

	private void afterExecution() {
		changeRound();
		loadButtons();
		loadRollen();		
	}

	private void hideButtons() {
		imageButton1.setVisibility(4);
		imageButton2.setVisibility(4);
		imageButton3.setVisibility(4);
		imageButton4.setVisibility(4);		
	}

	private void changeRound() {

		if (activeRolle == 1) {
			activeRolle = 2;
			myAnimal.setActivAbility(activeRolle);

		} else if (activeRolle == 2) {
			activeRolle = 1;
			myAnimal.setActivAbility(activeRolle);
		}
		loadButtons();
		loadRollen();
	}

	@Override
	public void onClick(View component) {
		int buttonId = component.getId();
		final int score = lebensBalkenEnemyProg.getProgress();

		switch (buttonId) {

		case R.id.abilityButton1:

			AlertDialog.Builder dialog1 = new AlertDialog.Builder(this);
			dialog1.setTitle(Html.fromHtml(myAnimal.getActivAbility()[0]
					.getName()));
			dialog1.setMessage(Html.fromHtml(myAnimal.getActivAbility()[0]
					.getDescription()));
			dialog1.setCancelable(false);
			dialog1.setIcon(getResources().getDrawable(
					myAnimal.getActivAbility()[0].getIconLink()));

			dialog1.setPositiveButton((" "),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							showStatus.setText("Warte auf den Gegner...");

							execute(myAnimal.getActivAbility()[0]);
						}

					});
			dialog1.setNegativeButton(" ",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							d.cancel();
						}

					});
			AlertDialog alertDialog1 = dialog1.create();

			alertDialog1.show();
			Button cancel = alertDialog1
					.getButton(DialogInterface.BUTTON_NEGATIVE);
			Button execute = alertDialog1
					.getButton(DialogInterface.BUTTON_POSITIVE);
			cancel.setBackgroundResource(R.drawable.btn_cancel);
			execute.setBackgroundResource(R.drawable.btn_execute);
			// TODO execute ability()
			break;
		case R.id.abilityButton2:

			// progressBar2.setProgress((int) (score*(1-dmgAnteil())));
			AlertDialog.Builder dialog2 = new AlertDialog.Builder(this);
			dialog2.setTitle(Html.fromHtml(myAnimal.getActivAbility()[1]
					.getName()));
			dialog2.setMessage(Html.fromHtml(myAnimal.getActivAbility()[1]
					.getDescription()));
			dialog2.setCancelable(false);
			dialog2.setIcon(getResources().getDrawable(
					myAnimal.getActivAbility()[1].getIconLink()));

			dialog2.setPositiveButton((" "),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							// progressBar2.setProgress((int) (progressBar2
							// .getProgress() * 0.25));
							showStatus.setText("Warte auf den Gegner...");
							execute(myAnimal.getActivAbility()[1]);
						}

					});
			dialog2.setNegativeButton(" ",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							d.cancel();
						}

					});
			AlertDialog alertDialog2 = dialog2.create();
			alertDialog2.show();
			Button cancel2 = alertDialog2
					.getButton(DialogInterface.BUTTON_NEGATIVE);
			Button execute2 = alertDialog2
					.getButton(DialogInterface.BUTTON_POSITIVE);
			cancel2.setBackgroundResource(R.drawable.btn_cancel);
			execute2.setBackgroundResource(R.drawable.btn_execute);
			break;
		case R.id.abilityButton3:
			AlertDialog.Builder dialog3 = new AlertDialog.Builder(this);
			dialog3.setTitle(Html.fromHtml(myAnimal.getActivAbility()[2]
					.getName()));
			dialog3.setMessage(Html.fromHtml(myAnimal.getActivAbility()[2]
					.getDescription()));
			dialog3.setCancelable(false);
			dialog3.setIcon(getResources().getDrawable(
					myAnimal.getActivAbility()[2].getIconLink()));

			dialog3.setPositiveButton((" "),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							showStatus.setText("Warte auf den Gegner...");
							execute(myAnimal.getActivAbility()[2]);
						}

					});
			dialog3.setNegativeButton(" ",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							d.cancel();
						}

					});
			AlertDialog alertDialog3 = dialog3.create();
			alertDialog3.show();
			Button cancel3 = alertDialog3
					.getButton(DialogInterface.BUTTON_NEGATIVE);
			Button execute3 = alertDialog3
					.getButton(DialogInterface.BUTTON_POSITIVE);
			cancel3.setBackgroundResource(R.drawable.btn_cancel);
			execute3.setBackgroundResource(R.drawable.btn_execute);
			// progressBar2.setProgress((int) (score*0.5));
			break;
		case R.id.abilityButton4:
			// Toast.makeText(getApplicationContext(), "button 4 is clicked",
			// Toast.LENGTH_SHORT).show();
			AlertDialog.Builder dialog4 = new AlertDialog.Builder(this);
			dialog4.setTitle(Html.fromHtml(myAnimal.getActivAbility()[3]
					.getName()));
			dialog4.setMessage(Html.fromHtml(myAnimal.getActivAbility()[3]
					.getDescription()));
			dialog4.setCancelable(false);
			dialog4.setIcon(getResources().getDrawable(
					myAnimal.getActivAbility()[3].getIconLink()));

			dialog4.setPositiveButton((" "),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							showStatus.setText("Warte auf den Gegner...");
							execute(myAnimal.getActivAbility()[3]);
						}

					});
			dialog4.setNegativeButton(" ",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface d, int id) {

							d.cancel();
						}

					});
			AlertDialog alertDialog4 = dialog4.create();
			alertDialog4.show();
			Button cancel4 = alertDialog4
					.getButton(DialogInterface.BUTTON_NEGATIVE);
			Button execute4 = alertDialog4
					.getButton(DialogInterface.BUTTON_POSITIVE);
			cancel4.setBackgroundResource(R.drawable.btn_cancel);
			execute4.setBackgroundResource(R.drawable.btn_execute);
			break;
		}

	}

}
